const express = require('express');
const cors = require('cors');
const app = express();

// 启用CORS
app.use(cors());
// 解析JSON请求体
app.use(express.json());

// 处理AI对话请求
app.post('/api/chat', async (req, res) => {
  try {
    const { messages } = req.body;
    
    // 调用火山方舟DeepSeek R1 API
    const response = await fetch('https://ark.cn-beijing.volces.com/api/v3/chat/completions', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer 你的token'
      },
      body: JSON.stringify({
        model: 'deepseek-r1-250120',
        messages: messages
      })
    });
    
    const data = await response.json();
    res.json(data);
  } catch (error) {
    console.error('API调用错误:', error);
    res.status(500).json({ error: '服务器错误' });
  }
});

// 静态文件服务
app.use(express.static('public'));

// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`服务器运行在 http://localhost:${PORT}`);
});